﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class CarrierRepository
    {
        // 添加载具
        public int AddCarrier(Carrier carrier)
        {
            string sql = @"INSERT INTO carrier 
                            (CarrierName, CarrierType, DetailedCarrierType, CarrierStatus, 
                             CarrierCapacity, LocationNumber, Quantity, CapacityStatus, 
                             LockStatus, CleaningStatus, TransportStatus) 
                           VALUES 
                            (@CarrierName, @CarrierType, @DetailedCarrierType, @CarrierStatus, 
                             @CarrierCapacity, @LocationNumber, @Quantity, @CapacityStatus, 
                             @LockStatus, @CleaningStatus, @TransportStatus)";

            MySqlParameter[] parameters = {
                new MySqlParameter("@CarrierName", carrier.CarrierName),
                new MySqlParameter("@CarrierType", carrier.CarrierType),
                new MySqlParameter("@DetailedCarrierType", carrier.DetailedCarrierType),
                new MySqlParameter("@CarrierStatus", carrier.CarrierStatus),
                new MySqlParameter("@CarrierCapacity", carrier.CarrierCapacity),
                new MySqlParameter("@LocationNumber", carrier.LocationNumber),
                new MySqlParameter("@Quantity", carrier.Quantity),
                new MySqlParameter("@CapacityStatus", carrier.CapacityStatus),
                new MySqlParameter("@LockStatus", carrier.LockStatus),
                new MySqlParameter("@CleaningStatus", carrier.CleaningStatus),
                new MySqlParameter("@TransportStatus", carrier.TransportStatus)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 删除载具
        public int DeleteCarrier(int carrierId)
        {
            string sql = "DELETE FROM carrier WHERE CarrierId = @CarrierId";
            MySqlParameter[] parameters = { new MySqlParameter("@CarrierId", carrierId) };
            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 更新载具信息
        public int UpdateCarrier(Carrier carrier)
        {
            string sql = @"UPDATE carrier 
                            SET CarrierName = @CarrierName, 
                                CarrierType = @CarrierType, 
                                DetailedCarrierType = @DetailedCarrierType, 
                                CarrierStatus = @CarrierStatus, 
                                CarrierCapacity = @CarrierCapacity, 
                                LocationNumber = @LocationNumber, 
                                Quantity = @Quantity, 
                                CapacityStatus = @CapacityStatus, 
                                LockStatus = @LockStatus, 
                                CleaningStatus = @CleaningStatus, 
                                TransportStatus = @TransportStatus 
                          WHERE CarrierId = @CarrierId";

            MySqlParameter[] parameters = {
                new MySqlParameter("@CarrierName", carrier.CarrierName),
                new MySqlParameter("@CarrierType", carrier.CarrierType),
                new MySqlParameter("@DetailedCarrierType", carrier.DetailedCarrierType),
                new MySqlParameter("@CarrierStatus", carrier.CarrierStatus),
                new MySqlParameter("@CarrierCapacity", carrier.CarrierCapacity),
                new MySqlParameter("@LocationNumber", carrier.LocationNumber),
                new MySqlParameter("@Quantity", carrier.Quantity),
                new MySqlParameter("@CapacityStatus", carrier.CapacityStatus),
                new MySqlParameter("@LockStatus", carrier.LockStatus),
                new MySqlParameter("@CleaningStatus", carrier.CleaningStatus),
                new MySqlParameter("@TransportStatus", carrier.TransportStatus),
                new MySqlParameter("@CarrierId", carrier.CarrierId)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 查询所有载具
        public List<Carrier> GetAllCarriers()
        {
            string sql = "SELECT * FROM carrier";
            return DBHelper.ExecuteQuery(sql, null, MapCarrier);
        }

        // 查询单个载具
        public Carrier GetCarrierById(int carrierId)
        {
            string sql = "SELECT * FROM carrier WHERE CarrierId = @CarrierId";
            MySqlParameter[] parameters = { new MySqlParameter("@CarrierId", carrierId) };
            var carriers = DBHelper.ExecuteQuery(sql, parameters, MapCarrier);
            return carriers.FirstOrDefault();
        }

        // 映射数据库查询结果到 Carrier 对象
        private Carrier MapCarrier(MySqlDataReader reader)
        {
            return new Carrier
            {
                CarrierId = (int)reader["CarrierId"],
                CarrierName = reader["CarrierName"].ToString(),
                CarrierType = reader["CarrierType"].ToString(),
                DetailedCarrierType = reader["DetailedCarrierType"].ToString(),
                CarrierStatus = reader["CarrierStatus"].ToString(),
                CarrierCapacity = (int)reader["CarrierCapacity"],
                LocationNumber = (int)reader["LocationNumber"],
                Quantity = (int)reader["Quantity"],
                CapacityStatus = reader["CapacityStatus"].ToString(),
                LockStatus = reader["LockStatus"].ToString(),
                CleaningStatus = reader["CleaningStatus"].ToString(),
                TransportStatus = reader["TransportStatus"].ToString()
            };
        }
    }
}